import { state, mutations } from '../../store'
import { mapGetters } from 'vuex'

export default {
  props: {
    form: Object,
    isActive: Boolean,
  },
  data () {
    return {
      state,
    }
  },
  computed: {
    ...mapGetters(['isPlatFormUser', 'isBelongAgencyId', 'isAdmin']),
    // 显示商品原价
    showOriginalCost () {
      const match = this.$enum.getEnumValueByLabel(this.$enum.ENUM_COMMODITY_SWITCH_PRICE_TYPE, '开启原价显示')
      return this.form.data?.openOriginalCost === match
    },
    // 显示总限量库存
    showTotalStock () {
      const match = this.$enum.getEnumValueByLabel(this.$enum.ENUM_COMMODITY_STOCK_TYPE, '总限量库存')
      const match1 = this.$enum.getEnumValueByLabel(this.$enum.ENUM_COMMODITY_STOCK_TYPE, '总限量库存 + 日限量库存')
      return this.form.data?.repertoryType === match || this.form.data?.repertoryType === match1
    },
    // 显示日限量库存
    showDayStock () {
      const match = this.$enum.getEnumValueByLabel(this.$enum.ENUM_COMMODITY_STOCK_TYPE, '日限量库存')
      const match1 = this.$enum.getEnumValueByLabel(this.$enum.ENUM_COMMODITY_STOCK_TYPE, '总限量库存 + 日限量库存')
      return this.form.data?.repertoryType === match || this.form.data?.repertoryType === match1
    },
    // 显示分享奖励金开关类型
    showShareRewardsSwitchType () {
      const match = this.$enum.getEnumValueByLabel(this.$enum.ENUM_COMMODITY_SHARE_REWARDS_SWITCH_TYPE, '有分享奖励金')
      return this.form.data?.exitDistr === match
    },
    // 显示分享奖励金类型 - 根据传入类型
    showShareRewardsType (type) {
      return type => {
        const match = this.$enum.getEnumValueByLabel(this.$enum.ENUM_COMMODITY_SHARE_REWARDS_RULER_TYPE, type)
        return this.form.data?.calculationRewards === match
      }
    },
    // 显示限购数量
    showLimitBugNum () {
      const match = this.$enum.getEnumValueByLabel(this.$enum.ENUM_COMMODITY_LIMIT_TYPE, '按用户Id限购')
      const match1 = this.$enum.getEnumValueByLabel(this.$enum.ENUM_COMMODITY_LIMIT_TYPE, '按订单限购')
      return this.form.data?.limitType === match || this.form.data?.limitType === match1
    },
    getTotalStock: {
      // 获取总限量库存 - 注意：库存总数在不同页面情况显示不同， 添加页面：总库限量库存  编辑/详情页面：总库限量库存余量（总库限量库存 - 已售数）
      // 设置总限量库存 -
      get () {
        // productTotal未填写时
        if (this.form.data.productTotal === null || this.form.data.productTotal === undefined) {
          return undefined
        }

        // 添加 && 复制页面
        if (this.form.status === 'c') {
          return this.form.data.productTotal
        }

        // 编辑 && 详情页面
        if (['r', 'u'].includes(this.form.status)) {
          const productTotal = parseInt(this.form.data.productTotal)
          const purchasedNumber = this.form.data.purchasedNumber ? parseInt(this.form.data.purchasedNumber) : 0
          return productTotal - purchasedNumber
        }
      },
      set (value) {
        // 未填写时
        if (value === null || value === undefined) {
          this.$set(this.form.data, 'productTotal', value)
          return
        }

        // 添加 && 复制页面
        if (this.form.status === 'c') {
          this.$set(this.form.data, 'productTotal', value)
          return
        }

        // 编辑 && 详情页面
        if (['r', 'u'].includes(this.form.status)) {
          const purchasedNumber = this.form.data.purchasedNumber ? parseInt(this.form.data.purchasedNumber) : 0
          this.$set(this.form.data, 'productTotal', value + purchasedNumber)
          return
        }
      }
    }
  },
}
